<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Admin\Service;

class ImageScoreService{
	
    /**
     * 保存图片和谱单的关系
     * @return unknown
     */
    public function save($image_ids, $score_id){
        if (empty($image_ids || empty($score_id))) {
            return 0;
        }
        //step-0: 先取消原首图
        $Model = M('ImageScore');
        $cond = array(
            'score_id' => $score_id,
            'is_cover' => 1
        );
        //取消原首图
        $is_cover = $Model->where($cond)->select();
        if (!empty($is_cover)) {
            $Model->where($cond)->save(array('is_cover'=>0));
        }
        
        //step-1: 组织图片和谱单的关系
        $image_score = array();
        foreach ($image_ids as $key=>$image_id) {
            $image_score[] = array(
                'score_id' => $score_id,
                'image_id' => $image_id,
                'creator'  => UID
            );
        }
        //默认第一张传入的为首图
        $image_score[0]['is_cover'] = 1;
        
        //step-2: 批量保存图片和谱单的关系
        return $Model->addAll($image_score);
        
	}


    public function delete($image_id, $score_id) {
        $map = array(
            "image_id" =>$image_id,
            "score_id" => $score_id
        );
        $data = array(
            'is_delete' => 1,
            'is_cover' => 0
        );
        $res = M('ImageScore')->where($map)->save($data);
        return $res;
    }
	
}